#!/bin/sh
# Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved.
# Description: get oracle info return info in result file
# Arguments: SCRIPT_ROOT_PATH=$1 PID=$2
# Returns: 0 is success, other is fail, result_file return sid info 
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
set +x

SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3

. "${SCRIPT_ROOT_PATH}"/oracle_sbin_func.sh

TMP_FILE="${STMP_PATH}/oracle_test_tmp_${PID}.txt"

PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`
test -z "$PARAM_CONTENT"                                            && exit_with_error "PARAM_FILE"

DBINSTANCE=`get_value "${PARAM_CONTENT}" InstanceName`
DBNAME=`get_value "${PARAM_CONTENT}" AppName`
DBUSER=`get_value "${PARAM_CONTENT}" UserName`
DBUSERPWD=`get_value "${PARAM_CONTENT}" Password`
ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORA_GRID_USER=`get_value "${PARAM_CONTENT}" GridInstallUser`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`

test "$DBNAME" = "ERROR_PARAM_INVALID"                           && exit_with_error "DBNAME"
test "$DBINSTANCE" = "ERROR_PARAM_INVALID"                       && exit_with_error "DBINSTANCE"
test "$DBUSER" = "ERROR_PARAM_INVALID"                           && exit_with_error "DBUSER"
test "$DBUSERPWD" = "ERROR_PARAM_INVALID"                        && exit_with_error "DBUSERPWD"


log_info "DBNAME=${DBNAME}; DBINSTANCE=$DBINSTANCE; DBUSER=$DBUSER"

#get oracle user info
get_oracle_user
#get oracle user shell type
get_oracle_user_shell_type
ret_code=$?
if [ -z "${ORA_DB_USER}" ] || [ -z "${ORA_DB_GROUP}" ]; then
    log_error "Get oracle user failed"
    exit $ret_code
fi

env_file_list=`get_user_env_file "${ORA_DB_USER}"`
ret_code=$?
if [ $ret_code -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit $ret_code
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
ret_code=$?
if [ $ret_code -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit $ret_code
fi

# check oracle install
check_oracle_is_install


# check oracle install type
check_oracle_install_type

#get oracle base path
get_oracle_base_path

#get oracle home path
get_oracle_home_path

log_info "Start to check oracle instance status."

database_role=`get_db_role "${DBINSTANCE}" "${DBUSER}" "${DBUSERPWD}"`

instance_status=`get_database_status "${DBINSTANCE}" "${DBUSER}" "${DBUSERPWD}"`
ret_code=$?
if [ $ret_code -ne 0 ]; then
    log_error "Get instance status failed."
    exit $ret_code
fi
instance_status=`rd_substr "$instance_status" 1 4`
if [ "${database_role}" = "PRIMARY" ]; then
    if [ "${instance_status}" != "OPEN" ]; then
        log_error "Instance status($instance_status) no open."
        exit "${ERROR_INSTANCE_NOSTART}"
    fi
else
    if [ "${instance_status}" != "OPEN" ] && [ "${instance_status}" != "MOUN" ]; then
        log_error "Instance status($instance_status) no open or mounted."
        exit "${ERROR_INSTANCE_NOSTART}"
    fi
fi

instance_mode=`get_database_open_mode "${DBINSTANCE}" "${DBUSER}" "${DBUSERPWD}"`
ret=${?}
if [ $ret -ne 0 ]; then
    log_error "Connect to database(${DBINSTANCE}) failed on test."
    exit $ret
fi
temp_value=`echo "${instance_mode}" | ${MYAWK} '{print $1}'`
mode_name=`echo "$temp_value" | tr '[a-z]' '[A-Z]'`
mode_name=`rd_substr "$mode_name" 1 4`
if [ "${database_role}" = "PRIMARY" ]; then
    if [ "${mode_name}" != "READ" ]; then
        exit "${RETURN_INVALID}"
    fi
else
    # 支持mounted状态
    if [ "${mode_name}" != "READ" ] && [ "${mode_name}" != "MOUN" ]; then
        exit "${RETURN_INVALID}"
    fi
fi
log_info "Test Oracle password and mode success"
